const { resContError, resContSuccess } = require('../../responseControl')
const { addCardIndex } = require('../../../utils/getCards/index')
const { queryCardById, querySeries, queryPackage, queryType, queryRare, queryCard, changeNumCard, clearDataBase } = require('../../../utils/queryCards/index')
//新增卡 
add = async (ctx, next) => {
  let { series, pack, like } = ctx.request.body
  if (!series || !pack || !like) {
    resContError(ctx, '请输入系列,卡包,编号')
    return false
  }
  try {
    let result = await addCardIndex(series, pack, like)
    resContSuccess(ctx, result)
  } catch (error) {
    console.log(error);
    resContError(ctx, `请输入正确的系列,卡包,编号信息`)
  }
}
//根据id查详情
queryCById = async (ctx, next) => {
  let { id } = ctx.request.query
  try {
    let result = await queryCardById(id)
    resContSuccess(ctx, result)
  } catch (error) {
    console.log(error);
    resContError(ctx, error.message)
  }

}
//获取所有多选信息
getQuery = async (ctx, next) => {
  try {
    let series = await querySeries()
    let package = await queryPackage()
    let type = await queryType()
    let rare = await queryRare()
    resContSuccess(ctx, {
      series,
      package,
      ...type,
      rare
    })
  } catch (error) {
    resContError('系统错误', error)
  }
}
//查询卡
queryC = async (ctx, next) => {
  let query = ctx.request.body
  try {
    let result = await queryCard(query)
    resContSuccess(ctx, result)
  } catch (error) {
    resContError(ctx, error.message)
  }
}
//修改数量
changeNumC = async (ctx, next) => {
  let arr = ctx.request.body
  if (arr.length === 0) {
    resContError(ctx, '请至少传入一组数据')
    return false
  }
  try {
    let result = await changeNumCard(arr)
    resContSuccess(ctx, result)
  } catch (error) {
    resContError(ctx, error, message || error)
  }
}
//清空数据库
clearDB = async (ctx, next) => {
  const { pwd } = ctx.request.body
  if (pwd !== 'dianchi123') {
    resContError(ctx, '密码错误,请勿再次尝试')
    return false
  }
  try {
    await clearDataBase()
    resContSuccess(ctx, '清空成功')
  } catch (error) {
    resContError(ctx, '系统异常,请勿再次尝试并练习管理员', error)
  }
}
module.exports = {
  add, queryCById, getQuery, queryC, changeNumC, clearDB
}